using System;
using System.Collections;
using System.Text;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;

namespace OracleInternal.TTC
{
	internal class TTCFunction : TTCMessage
	{
		internal const short OOPEN = 2;

		internal const short OEXEC = 4;

		internal const short OFETCH = 5;

		internal const short OCLOSE = 8;

		internal const short OLOGOFF = 9;

		internal const short OCOMON = 12;

		internal const short OCOMOFF = 13;

		internal const short OCOMMIT = 14;

		internal const short OROLLBACK = 15;

		internal const short OCANCEL = 20;

		internal const short ODSCRARR = 43;

		internal const short OVERSION = 59;

		internal const short OK2RPC = 67;

		internal const short OALL7 = 71;

		internal const short OSQL7 = 74;

		internal const short OEXFEN = 78;

		internal const short O3LOGON = 81;

		internal const short O3LOGA = 82;

		internal const short OKOD = 92;

		internal const short OALL8 = 94;

		internal const short OLOBOPS = 96;

		internal const short ODNY = 98;

		internal const short OTXSE = 103;

		internal const short OTXEN = 104;

		internal const short OCCA = 105;

		internal const short O80SES = 107;

		internal const short OAUTH = 115;

		internal const short OSESSKEY = 118;

		internal const short OCANA = 120;

		internal const short OKPN = 125;

		internal const short OPING = 147;

		internal const short OSCID = 135;

		internal const short OSESSGET = 162;

		internal const short OSESSRLS = 163;

		internal const short OSESSSTATE = 176;

		internal const short OCHUNKINFO = 190;

		internal const short OAQNFY = 187;

		internal short m_functionCode;

		private byte m_sequenceNumber;

		private int[] retLen = new int[1];

		internal TTCFunction(MarshallingEngine marshallingEngine, short functionCode, byte sequenceNumber)
			: base(marshallingEngine, 3)
		{
			m_functionCode = functionCode;
			m_sequenceNumber = sequenceNumber;
		}

		internal void WriteFunctionHeader()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCFunction, OracleTraceFuncName.WriteFunctionHeader);
			}
			try
			{
				WriteTTCCode();
				m_marshallingEngine.MarshalUB1(m_functionCode);
				m_marshallingEngine.MarshalUB1(m_sequenceNumber);
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCFunction, OracleTraceFuncName.WriteFunctionHeader, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCFunction, OracleTraceFuncName.WriteFunctionHeader);
				}
			}
		}

		internal void ProcessOCSSYNCMessage(Hashtable sProperties)
		{
			try
			{
				int num = (int)m_marshallingEngine.UnmarshalUB4();
				m_marshallingEngine.UnmarshalUB1();
				if (num > 0)
				{
					TTCKeywordValuePair[] array = new TTCKeywordValuePair[num];
					StringBuilder stringBuilder = new StringBuilder();
					for (int i = 0; i < num; i++)
					{
						array[i] = TTCKeywordValuePair.Unmarshal(m_marshallingEngine);
						if (ProviderConfig.m_bTraceLevelPrivate)
						{
							if (array[i].m_keyword >= 0)
							{
								stringBuilder.Append("[" + array[i].m_keyword + "]" + m_marshallingEngine.m_connImplReference.GetCorrespondingAuthAttrName(array[i].m_keyword) + " ==> ");
							}
							if (array[i].m_binaryValue != null)
							{
								stringBuilder.Append(m_marshallingEngine.m_dbCharSetConv.ConvertBytesToString(array[i].m_binaryValue, 0, array[i].m_binaryValue.Length));
							}
						}
					}
					if (ProviderConfig.m_bTraceLevelPrivate)
					{
						Trace.Write(OracleTraceLevel.Private, OracleTraceTag.OCSSYNC, OracleTraceClassName.TTCFunction, OracleTraceFuncName.ProcessOCSSYNCMessage, stringBuilder.ToString());
					}
					m_marshallingEngine.m_connImplReference.UpdateSessionAttributes(array, sProperties);
				}
				else
				{
					TTCKeywordValuePair[] array = null;
				}
				m_marshallingEngine.UnmarshalUB4(bIgnoreData: true);
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCFunction, OracleTraceFuncName.ProcessOCSSYNCMessage);
				}
			}
		}

		internal void ProcessServerSidePiggybackFunction(byte opCode = 0)
		{
			if (opCode == 0)
			{
				opCode = (byte)m_marshallingEngine.UnmarshalUB1();
			}
			switch (opCode)
			{
			case 7:
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, OracleTraceTag.TTC, OracleTraceClassName.TTCFunction, OracleTraceFuncName.ProcessServerSidePiggybackFunction, "Received Server Piggy Back Message: OCLTXID");
				}
				long num = m_marshallingEngine.UnmarshalUB4();
				if (num > 0)
				{
					if (m_marshallingEngine.m_ltxId == null || m_marshallingEngine.m_ltxId.Length != num)
					{
						m_marshallingEngine.m_ltxId = new byte[num];
					}
					m_marshallingEngine.UnmarshalCLR(m_marshallingEngine.m_ltxId, 0, retLen);
				}
				break;
			}
			case 1:
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, OracleTraceTag.TTC, OracleTraceClassName.TTCFunction, OracleTraceFuncName.ProcessServerSidePiggybackFunction, "Received Server Piggy Back Message: OCQCINV");
				}
				break;
			case 2:
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, OracleTraceTag.TTC, OracleTraceClassName.TTCFunction, OracleTraceFuncName.ProcessServerSidePiggybackFunction, "Received Server Piggy Back Message: OCOSPID");
				}
				int n = m_marshallingEngine.UnmarshalUB2();
				m_marshallingEngine.UnmarshalUB1(bIgnoreData: true);
				m_marshallingEngine.UnmarshalNBytes_ScanOnly(n);
				break;
			}
			case 4:
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, OracleTraceTag.TTC, OracleTraceClassName.TTCFunction, OracleTraceFuncName.ProcessServerSidePiggybackFunction, "Received Server Piggy Back Message: OCSESSRET");
				}
				if (m_marshallingEngine.m_drcpSessionReturnValues == null)
				{
					m_marshallingEngine.m_drcpSessionReturnValues = new TTCSessionReturnValues(m_marshallingEngine);
				}
				m_marshallingEngine.m_drcpSessionReturnValues.Receive();
				break;
			case 5:
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, OracleTraceTag.TTC, OracleTraceClassName.TTCFunction, OracleTraceFuncName.ProcessServerSidePiggybackFunction, "Received Server Piggy Back Message: OCSSYNC");
				}
				m_marshallingEngine.UnmarshalUB2(bIgnoreData: true);
				m_marshallingEngine.UnmarshalUB1(bIgnoreData: true);
				ProcessOCSSYNCMessage(null);
				break;
			case 9:
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, OracleTraceTag.TTC, OracleTraceClassName.TTCFunction, OracleTraceFuncName.ProcessServerSidePiggybackFunction, "Received Server Piggy Back Message: OCXSSS2");
				}
				m_marshallingEngine.UnmarshalUB2(bIgnoreData: true);
				m_marshallingEngine.UnmarshalUB1(bIgnoreData: true);
				break;
			case 8:
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, OracleTraceTag.TTC, OracleTraceClassName.TTCFunction, OracleTraceFuncName.ProcessServerSidePiggybackFunction, "Received Server Piggy Back Message: OCAPPCONTCTL");
				}
				m_marshallingEngine.UnmarshalUB2(bIgnoreData: true);
				m_marshallingEngine.UnmarshalUB1(bIgnoreData: true);
				m_marshallingEngine.UnmarshalUB4(bIgnoreData: true);
				m_marshallingEngine.UnmarshalUB4(bIgnoreData: true);
				m_marshallingEngine.UnmarshalUB1(bIgnoreData: true);
				m_marshallingEngine.UnmarshalDALC(bIgnoreData: true);
				break;
			case 3:
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, OracleTraceTag.TTC, OracleTraceClassName.TTCFunction, OracleTraceFuncName.ProcessServerSidePiggybackFunction, "Received Server Piggy Back Message: OCTRCEVT");
				}
				break;
			default:
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, OracleTraceTag.TTC, OracleTraceClassName.TTCFunction, OracleTraceFuncName.ProcessServerSidePiggybackFunction, "Received Server Piggy Back Message: UNKNOWN (protocol violation)");
				}
				break;
			}
		}
	}
}
